<?php
//声明命名空间
namespace Home\Controller;
use Frame\Frame;
use Home\Model\ArticleModel;
use Home\Model\CategoryModel;
use Home\Model\LinksModel;
use Frame\Libs\BaseController;
use Home\Model\MessageModel;

//定义最终的首页控制器类，并继承基础控制器类
final class IndexController extends BaseController{
	//首页显示方法
	public function index()
	{
	    //获取友情链接数据
	    $links = LinksModel::getInstance()->fetchAll();

        //获取分类数据
        $Categorys = CategoryModel::getInstance()->CategoryList(
            //获取带文章数量的分类数据
            CategoryModel::getInstance()->fetchAllCount()
        );
        //按照时间归档文章数据 例如 2018年6月(5)
        $Month = ArticleModel::getInstance()->fetchAllMonth();

        //首页推荐文章
        $Article = ArticleModel::getInstance()->fetchAllArticle("2>1",0,4,"desc");
        //向视图传递值
        $this->smarty->assign([
            "links" => $links,
            "categorys" => $Categorys,
            "month" => $Month,
            "article" => $Article
        ]);
        //显示视图
        $this->smarty->display("Index/index.html");
	}
	//显示列表页方法
	public function lists()
    {
        //获取友情链接数据
        $links = LinksModel::getInstance()->fetchAll();

        //构建搜索条件
        $where = "2>1";
        if(isset($_POST["title"])) $where .= " and title='".$_POST["title"]."'";
        if(isset($_GET["category_id"])) $where .= " and category_id=".$_GET["category_id"];

        //构建分页参数
        $pagesize = 3;
        $page = isset($_GET["page"]) ? $_GET["page"] : 1;
        $startRow = ($page-1)*$pagesize;
        $records = ArticleModel::getInstance()->rowCount($where);
        $params = [
            "c" => CONTROLLER,
            "a" => ACTION
        ];
        if(isset($_POST["title"])) $params["title"] = $_POST["title"];
        if(isset($_GET["category_id"])) $params["category_id"] = $_GET["category_id"];

        //获取分类数据
        $Categorys = CategoryModel::getInstance()->CategoryList(
        //获取带文章数量的分类数据
            CategoryModel::getInstance()->fetchAllCount()
        );
        //按照时间归档文章数据 例如 2018年6月(5)
        $Month = ArticleModel::getInstance()->fetchAllMonth();

        //文章连表获取数据
        $Article = ArticleModel::getInstance()->fetchAllArticle($where,$startRow,$pagesize);
        //获取分页字符串
        $page = new \Frame\Vendor\Pager($records,$pagesize,$page,$params);
        $pages = $page->showPage();

        //向视图传递值
        $this->smarty->assign([
            "links"     => $links,
            "categorys" => $Categorys,
            "month"     => $Month,
            "article"   => $Article,
            "pages"     => $pages
        ]);
        //显示视图
        $this->smarty->display("Index/lists.html");
    }
    public function content()
    {
        //显示前段博文详细页方法
        $id = $_GET["id"];

        //获取友情链接数据
        $links = LinksModel::getInstance()->fetchAll();

        //获取分类数据
        $Categorys = CategoryModel::getInstance()->CategoryList(
        //获取带文章数量的分类数据
            CategoryModel::getInstance()->fetchAllCount()
        );
        //按照时间归档文章数据 例如 2018年6月(5)
        $Month = ArticleModel::getInstance()->fetchAllMonth();

        //阅读数的更新
        ArticleModel::getInstance()->updateRead($id);

        //上一篇和下一篇  5  8
        $arr[] = ArticleModel::getInstance()->fetchOneArticle("article.id<$id","article.id desc");
        $arr[] = ArticleModel::getInstance()->fetchOneArticle("article.id>$id","article.id asc");

        ////文章连表获取数据
        $Article = ArticleModel::getInstance()->fetchOneArticle("article.id=$id");

        //向视图传递值
        $this->smarty->assign([
            "links" => $links,
            "categorys" => $Categorys,
            "month" => $Month,
            "article" => $Article,
            "arr"   => $arr
        ]);
        //显示视图
        $this->smarty->display("Index/article.html");
    }
    public function praise()
    {
		//点赞功能
        if(!isset($_COOKIE["praise"])){
            $id = $_GET["id"];
            //增加点赞数
            ArticleModel::getInstance()->updatePraise($id);
            $setTime = 3600*24;
            setcookie("praise","praise_check",time()+$setTime);
            echo "OK";
        }else{
            echo "NO";
        }
    }
	public function about()
	{
		//友情链接
		$links = LinksModel::getInstance()->fetchAll();

		$this->smarty->assign("links",$links);

		$this->smarty->display("Index/about.html");
	}
	public function message()
	{
		//显示留言板
        $Messages = MessageModel::getInstance()->fetchAll();
        //友情链接数据
        $Links = LinksModel::getInstance()->fetchAll();
        $this->smarty->assign([
            "links" => $Links,
            "messages" => $Messages
        ]);
		$this->smarty->display("Index/message.html");
	}
	public function doAddMessage()
    {
        $data["name"] = htmlspecialchars($_POST["name"],ENT_QUOTES);
        $data["email"] = htmlspecialchars($_POST["email"],ENT_QUOTES);
        $data["content"] = htmlspecialchars($_POST["content"],ENT_QUOTES);
        $data["pubdate"] = time();
        $Message = MessageModel::getInstance()->insert($data);
        if($Message){
            $this->jump("留言成功","?c=Index&a=message");
        }else{
            $this->jump("留言失败","?c=Index&a=message");
        }
    }
}